<?php

namespace api\admin;

class driver_comment
{
    //分页列表
    public function getList($data){
        $type=(int)$data['type'];
        $page=(int)$data['page'];
        $size=(int)$data['size'];
        $start=($page-1)*$size;
        $driver_name=$data['driver_name'];
        if(!empty($data['time'])){
            $start_time=strtotime($data['time'][0]);
            $end_time=strtotime($data['time'][1]);
        }else{
            $start_time=0;
            $end_time=0;
        }

        $where=" where a.status=1 and a.is_delete=1";
        if($type!==0){
            $where .=" and a.type=$type";
        }
        if(!empty($driver_name)){
            $where .=" and b.name like'%$driver_name%'";
        }
        if($start_time!==0){
            $where .=" and (a.create_time between $start_time and $end_time)";
        }
        $limit=" order by a.id desc limit $start,$size";
        $sql="SELECT a.*,b.name as driver_name,b.phone as driver_phone,c.nickname,c.avatar,c.name as user_name,c.phone as user_phone FROM `driver_record` a left join driver b on (a.driver_id=b.id) left join user c on (a.user_id=c.id)";
        $con=con();
        $data=mysqli_query($con,$sql.$where.$limit);
        $data=mysqli_fetch_all($data,true);
        $total=mysqli_query($con,$sql.$where);
        $total=mysqli_num_rows($total);
        mysqli_close($con);
        for($i=0;$i<count($data);$i++){
            $data[$i]['nickname']=urldecode($data[$i]['nickname']);
            if(empty($data[$i]['img_url'])){
                $data[$i]['img_url']=[];
            }else{
                $data[$i]['img_url']=explode(",",$data[$i]['img_url']);
            }
            switch((int)$data[$i]['type']){
                case 1:
                    $data[$i]['type_text']='一星';
                    break;
                case 2:
                    $data[$i]['type_text']='二星';
                    break;
                case 3:
                    $data[$i]['type_text']='三星';
                    break;
                case 4:
                    $data[$i]['type_text']='四星';
                    break;
                case 5:
                    $data[$i]['type_text']='五星';
                    break;
            }
        }
        return out_right([
            'data'=>$data,
            'page'=>$page,
            'size'=>$size,
            'total'=>$total,
            'total_page'=>ceil($total/$size),
        ]);
    }
}